package com.cnebula.uas.sso.idp.impl;

import java.net.URISyntaxException;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.http.client.utils.URIBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cnebula.uas.sso.ConfAdmin;
import com.cnebula.uas.sso.idp.DelegateIDP;

/**
 * 深圳大学 IDP
 *
 */
@Service
public class DelegateIDP510500 implements DelegateIDP{

	@Autowired
	ConfAdmin conf;

	String callbackURL;
	String casURL;

	@PostConstruct
	public void init() {
		callbackURL = "http://" + conf.host + ":" + conf.port + "/uas/sso/next/idp/510500/callback";
		casURL = "http://" + conf.okapiHost + ":" + conf.okapiPort + "/_/invoke/tenant/"+conf.supertenant+"/cas/regenerate";
	}

	public String idp(String sp, String gotoURL) throws URISyntaxException {
		URIBuilder idpCBURLBuilder = new URIBuilder(callbackURL);
		idpCBURLBuilder.addParameter("sp", sp);
		idpCBURLBuilder.addParameter("goto", gotoURL);

		URIBuilder casURLBuilder = new URIBuilder(casURL);
		casURLBuilder.addParameter("rurl", idpCBURLBuilder.toString());
		
		return casURLBuilder.toString();
	}
	
	public String callback(String sp, String idpCode, String gotoURL, Map<String, String> params ) throws URISyntaxException{
		
		
		String okapiToken =  params.get("ok_tokean");
		
		URIBuilder builder = new URIBuilder(sp);
		builder.addParameter("idp", idpCode);
		builder.addParameter("artifact", okapiToken);
		builder.addParameter("goto", gotoURL);
		
		return builder.toString();
	}
}
